import { GCtrl } from "../../../Core/GCtrl";
import { GListView, GListViewDir } from "../../../Core/GView/GListView";
import GViewBase from "../../../Core/GView/GViewBase";
import { CMsg, CURRENCY, ITEM_TYPE, RPointMask } from "../../Common/Define";
import { Res } from "../../Common/UIResources";
import RJXItem from "../../Data/RunTime/RunRaw/RJXItem";
import GameMgr from "../../Logic/GameMgr";
import OfficerItem from "./OfficerItem";

const { ccclass, property, menu, executeInEditMode } = cc._decorator;
@ccclass @menu('View/Home/OfficerCtrl')

export default class OfficerCtrl extends GViewBase {
    @property(cc.ScrollView) cardView: cc.ScrollView = null;
    @property(cc.Node) content: cc.Node = null;
    @property(cc.Node) mask: cc.Node = null;
    private _listView: GListView = null;
    private _officerData: Array<SOfficerDataRaw> = null;
    private _offAllData = [];
    onGLoad() {
        this.node.setContentSize(cc.winSize);
        this.initEmbattle();
        GCtrl.ES.on(CMsg.client.bag.onCurrencyChange, this, this.initEmbattle.bind(this));
    }

    /**初始化View */
    initEmbattle() {
        GameMgr.redMgr.sendValueSettingMsg(RPointMask.RPM_Office, false);
        let allData: Array<SOfficerDataRaw> = GameMgr.officerData.data.values<SOfficerDataRaw>();
        allData.forEach((value) => {
            this._offAllData.push(value.exp);
        })
        this._officerData = GameMgr.officerData.data.values();
        if (!this._listView) {
            this.assetImpl.prefab(Res.prefab.item.officerItem, (prefab: cc.Prefab) => {
                this._listView = new GListView({
                    scrollview: this.cardView,
                    mask: this.mask,
                    content: this.content,
                    itemTpl: cc.instantiate(prefab),
                    itemSetter: this.onItemSetter.bind(this),
                    direction: GListViewDir.Vertical,
                    column: 0,
                    gapY: 0,
                    padingY: 0,
                    padingX: 0,
                    isCbClass: true,
                    isWidget: true,
                });
                this._listView.setData(this._officerData);
            })
        } else {
            this._listView.setData(this._officerData);
        }
    }

    onItemSetter(item: OfficerItem, data: SOfficerDataRaw, index: number) {
        item.node.zIndex = this._offAllData.length - index;
        item.init(data, index, this._offAllData);
    }

    onClose() {
        super.onClose();
    }

    /**添加测试战勋 */
    protected addExploit() {
        let FEATS = new RJXItem([CURRENCY.FEATS, ITEM_TYPE.CURRENCY, 500]);
        let result = GameMgr.itemMgr.testCost(FEATS);
        if (result) {
            GCtrl.ES.emit(CMsg.client.bag.onBagItemChange, [[CURRENCY.FEATS, ITEM_TYPE.CURRENCY, 100]])
        }
    }
}   